import { formatInt, isNaNOrNull } from './number-util'

export const DAY = 3600 * 24
export const HOUR = 3600
export const MINUTE = 60

const MAX = 2147483647
export const TimeConst = {
  MAX,
  NONE: MAX,
  MIN: -MAX
}

export function timeValid(t: number): boolean {
  return t > TimeConst.MIN && t < TimeConst.MAX
}

export function timeInvalid(t: number): boolean {
  return t <= TimeConst.MIN || t >= TimeConst.MAX
}

export function time2dHHMMss(
  t: number,
  invalid: number = TimeConst.NONE,
  offset: number = 0
): string {
  if (isNaNOrNull(t)) {
    return ''
  }
  if (t <= -invalid || t >= invalid) {
    return ''
  }
  t = Math.floor(t! + offset)
  const d = Math.floor(t / DAY)
  const h = Math.floor(t / HOUR) % 24
  const m = Math.floor(t / MINUTE) % 60
  const s = t % MINUTE
  let ret: string
  if (d !== 0) {
    ret = `${d} `
  } else {
    ret = ''
  }
  ret += `${h}`.padStart(2, '0') + ':' + `${m}`.padStart(2, '0')
  if (s !== 0) {
    ret = ret + ':' + `${s}`.padStart(2, '0')
  }
  return ret
}

export function time2dHHMMSS(
  t: number,
  invalid: number = TimeConst.NONE,
  offset: number = 0
): string {
  if (isNaNOrNull(t)) {
    return ''
  }
  if (t <= -invalid || t >= invalid) {
    return ''
  }
  t = Math.floor(t! + offset)
  const d = Math.floor(t / DAY)
  const h = Math.floor(t / HOUR) % 24
  const m = Math.floor(t / MINUTE) % 60
  const s = t % MINUTE
  let ret: string
  if (d !== 0) {
    ret = `${d} `
  } else {
    ret = ''
  }
  ret += `${h}`.padStart(2, '0') + ':' + `${m}`.padStart(2, '0') + ':' + `${s}`.padStart(2, '0')
  return ret
}

export function time2str(t: number, invalid: number = TimeConst.NONE, offset: number = 0): string {
  if (isNaNOrNull(t)) {
    return ''
  }
  if (t <= -invalid || t >= invalid) {
    return ''
  }
  t = Math.floor(t! + offset)
  const d = Math.floor(t / DAY)
  if (d < 0) {
    t -= d * DAY
  }
  const h = Math.floor(t / HOUR) % 24
  const m = Math.floor(t / MINUTE) % 60
  const s = t % MINUTE
  // return d + ' ' + formatInt(h) + ':' + formatInt(m) + ':' + formatInt(s)
  return (
    d +
    ' ' +
    `${h}`.padStart(2, '0') +
    ':' +
    `${m}`.padStart(2, '0') +
    ':' +
    `${s}`.padStart(2, '0')
  )
}

const timeRegexp = /(([\d]+) )?(\d{2}):(\d{2})(:(\d{2}))?/

export function str2time(
  str: string,
  invalid: number = TimeConst.NONE,
  offset: number = 0
): number {
  if (!str) {
    return invalid
  }
  str = str.trim()
  if (str === '') {
    return invalid
  }
  let r = timeRegexp.exec(str)
  if (!r || r[0].length !== str.length) {
    return invalid
  }
  let d = 0
  if (r[2]) {
    d = parseInt(r[2])
  }
  let h = parseInt(r[3])
  let m = parseInt(r[4])
  let s = 0
  if (r[5]) {
    s = parseInt(r[6])
  }
  if (!isNaN(d) && h >= 0 && h < 24 && m >= 0 && m < 60 && s >= 0 && s < 60) {
    return d * DAY + h * HOUR + m * MINUTE + s + offset
  }
  return invalid
}

interface TimeRange {
  start: number
  end: number
}

export function timeRangeEqual(t0: TimeRange | undefined, t1: TimeRange | undefined): boolean {
  if (t0 === t1) {
    return true
  }
  if (!t0 && !t1) {
    return true
  }
  if (!t0 || !t1) {
    return false
  }
  return t0.start === t1.start && t0.end === t1.end
}

export function timeRange2str(tr: TimeRange, invalid: number = TimeConst.NONE): string {
  return time2str(tr.start, invalid) + '-' + time2str(tr.end, invalid)
}

export enum TimeFormat {
  DHHMMSS = 0,
  dHHMMSS = 1,
  dHHMMss = 2,
  DHHMM = 3,
  dHHMM = 4,
  HHMM = 5
}
